/*
 *      This program is free software; you can redistribute it and/or modify
 *      it under the terms of the GNU General Public License as published by
 *      the Free Software Foundation; either version 2 of the License, or
 *      (at your option) any later version.
 *      
 *      This program is distributed in the hope that it will be useful,
 *      but WITHOUT ANY WARRANTY; without even the implied warranty of
 *      MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *      GNU General Public License for more details.
 *      
 *      You should have received a copy of the GNU General Public License
 *      along with this program; if not, write to the Free Software
 *      Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
 *      MA 02110-1301, USA.
 */
#include "Drawing.hpp"
#include <vector>

//This tells us the max width and height of the map we can show at a time
#define MAXSHOWNX 800
#define MAXSHOWNY 600
class cMap
{
	public:
	int Width, Height;
	
	class cObject
	{
		public:
			int X, Y, Type;
			int W, H;
			bool Maintain;
			tex Image;
			cObject(char *Filename, int _X, int _Y, int _Type)
			{
				X = _X;
				Y = _Y;
				Type = _Type;
				Image = LoadTexture(Filename);
				W = Image.Width;
				H = Image.Height;
				Maintain = false;
			}
	};
	bool ShowPath;
	bool *Path; // Changed to Array since std::vectors are slow as hell
	std::vector<cObject> Objects;
	bool ObjectValid(int _Object)
	{
		if(!Objects[_Object].Image.Tex){
			Objects.erase(Objects.begin() + _Object);
			return false;
		}
		return true;
	}
	// TODO: Handle maps with widths and heights bigger than 800x600!
	cMap()
	{
		Width = 800;
		Height = 600;
		ShowPath = false;
		Path = (bool*)malloc(sizeof(bool) * Width*Height);
		for(int y = 0; y < Height; ++y)
			for(int x = 0; x < Width; ++x)
				Path[y*Width + x] = false;
	}
	~cMap()
	{
		free(Path);
	}
	// Changes the Dimensions for the map, should only be called from Map editor
	// WARNING: UNTESTED!
	void ChangeDimensions(int _Width, int _Height)
	{
		int OldW = Width;
		int OldH = Height;
		Width = _Width;
		Height = _Height;
		bool *TPath = Path;
		Path = (bool*)malloc(Width * Height * sizeof(bool));
		for(int Y = 0; Y < OldH; ++Y)
			for(int X = 0; X < OldW; ++X)
			{
				// If both are larger, that means we are past the entire array
				if(X > Width && Y > Height)
					return;
				// If only one is larger, that means that one of them are bigger, but the other isn't
				// So we should just continue so we get all the data
				if(X > Width || Y > Height)
					continue;
				Path[Y * OldW + X] = TPath[Y * OldW + X];
			}
		free(TPath); // Gotta free the temp one
	}
};
